//
//  COBalanceAlertView.swift
//  CocoProject
//
//  Created by Vill on 2023/11/13.
//

import UIKit

typealias balanceCertainBlock = (() -> Void)

class COBalanceAlertView: COBaseAlertView {
    var certainBlock:balanceCertainBlock?
    
    lazy var topView: UIView = {
        let top = UIView()
        top.backgroundColor = .white
        top.layer.cornerRadius = PTW(20)
        top.layer.masksToBounds = true
        return top
    }()

    lazy var tipLbl: UILabel = {
        let tip = UILabel()
        tip.text = "Your Balance"
        tip.textColor = UIColor.colorWithHex("#2C255B")
        tip.font = UIFont.pingfangFont(ofSize: PTW(25), weight: .heavy)
        return tip
    }()
    
    lazy var iconImg: UIImageView = {
        let icon = UIImageView()
        icon.image = KImaged("coco_mine_star")
        return icon
    }()
    
    lazy var coinsLbl: UILabel = {
        let tip = UILabel()
        tip.textColor = UIColor.colorWithHex("#2C255B")
        tip.textAlignment = .center
        tip.font = UIFont.pingfangFont(ofSize: PTW(18), weight: .heavy)
        return tip
    }()
    
    lazy var detailLbl: UILabel = {
        let tip = UILabel()
        tip.textColor = UIColor.colorWithHex("#7863A6")
        tip.textAlignment = .center
        tip.numberOfLines = 0
        tip.font = UIFont.pingfangFont(ofSize: PTW(15), weight: .medium)
        return tip
    }()
    
    lazy var rechargeBtn: UIButton = {
        let recharge = UIButton(type: .custom)
        recharge.setTitle("Recharge", for: .normal)
        recharge.setTitleColor(.white, for: .normal)
        recharge.backgroundColor = UIColor.gradientFromColor(colors: [UIColor.colorWithHex("#7038FF").cgColor,UIColor.colorWithHex("#FF3DE1").cgColor], direction: .Horizontal, range: PTW(225))
        recharge.layer.cornerRadius = PTW(65/2)
        recharge.layer.masksToBounds = true
        recharge.titleLabel?.font = UIFont.pingfangFont(ofSize: PTW(17), weight: .medium)
        recharge.addTarget(self, action: #selector(rechargeAction), for: .touchUpInside)
        return recharge
    }()
    
    init(coins:NSInteger,subtitle:String){
        super.init(type: .Center)
        self.contentView?.backgroundColor = UIColor.clear
        self.contentView?.addSubview(self.topView)
        self.topView.snp.makeConstraints { make in
            make.left.top.right.equalTo(self.contentView!)
            make.height.equalTo(PTW(380))
            make.bottom.equalTo(self.contentView!)
        }
        self.topView.addSubview(self.tipLbl)
        self.tipLbl.snp.makeConstraints { make in
            make.top.equalTo(self.topView).offset(PTW(25))
            make.centerX.equalTo(self.topView)
            make.height.equalTo(PTW(30))
        }
        self.topView.addSubview(self.iconImg)
        self.iconImg.snp.makeConstraints { make in
            make.size.equalTo(PTW(65))
            make.centerX.equalTo(self.topView)
            make.top.equalTo(self.tipLbl.snp.bottom).offset(PTW(50))
        }
        self.topView.addSubview(self.coinsLbl)
        self.coinsLbl.snp.makeConstraints { make in
            make.height.equalTo(PTW(22))
            make.centerX.equalTo(self.topView)
            make.top.equalTo(self.iconImg.snp.bottom).offset(PTW(2))
        }
        self.topView.addSubview(self.detailLbl)
        self.detailLbl.snp.makeConstraints { make in
            make.height.equalTo(PTW(50))
            make.centerX.equalTo(self.topView)
            make.top.equalTo(self.coinsLbl.snp.bottom).offset(PTW(40))
        }
        self.topView.addSubview(self.rechargeBtn)
        self.rechargeBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: PTW(225), height: PTW(65)))
            make.centerX.equalTo(self.topView)
            make.bottom.equalTo(self.topView.snp.bottom).offset(-PTW(30))
        }
        self.coinsLbl.text = "\(coins) coins"
        self.detailLbl.text = subtitle
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        DispatchQueue.main.async {
            self.contentView?.backgroundColor = UIColor.clear
        }
    }
    
    @objc func rechargeAction(){
        self.dissMiss()
        if ((self.certainBlock) != nil){
            self.certainBlock!()
        }
    }
    
    override func closeAction(){
        self.dissMiss()
    }
}
